package com.bgrc.hubu12305.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.bgrc.hubu12305.common.Result;
import com.bgrc.hubu12305.common.utils.UUIDUtils;
import com.bgrc.hubu12305.dao.SeatDao;
import com.bgrc.hubu12305.entity.OrderEntity;
import com.bgrc.hubu12305.entity.UserEntity;
import com.bgrc.hubu12305.service.OrderService;
import com.bgrc.hubu12305.service.SeatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 包名:com.bgrc.hubu12305.controller
 * 用户:86182
 * 时间:20211216:022021/12/16
 */
@RestController
@Api(tags = "订单接口")
@RequestMapping("order")
public class OrderController {

    @Autowired
    private SeatService seatService;

    @Autowired
    private OrderService orderService;
    //通过车次查询不同等级座位票价
    @GetMapping("/reserveTicket")
    @ApiOperation(value = "预定选座")
    public Result reserveTicket(@RequestParam String trainNumber,@RequestParam(value = "distance") Integer distance){

        List<Map<String, Integer>> seatPrice = seatService.queryPrice(trainNumber);

        for (Map<String,Integer> map:seatPrice
             ) {
            Integer price = map.get("price")*distance;
            map.put("price",price);
        }

        //Integer price = distance
        return Result.ok(seatPrice);

    }

    //提交订单请求
    @GetMapping("/buyTicket")
    @ApiOperation(value = "支付")
    public Result buyTicket(@RequestParam String seatType,
                           @RequestParam String trainNumber,
                           @RequestParam String startStation,
                           @RequestParam String endStation,
                           @RequestParam(value = "price") Integer price){

        Subject subject = SecurityUtils.getSubject();
        UserEntity userEntity = (UserEntity) subject.getPrincipal();
        /*String userName = userEntity.getUserName();
        String userTel = userEntity.getTel();
        String identityCard = userEntity.getIdentityCard();*/
        //根据seatType和trainNumber查询符合要求的车厢
        List<Map<String, Object>> maps = seatService.queryCarriages(seatType, trainNumber);
        //默认先分配剩余人数最多的车厢,已经降序排列
        String carriages_no = maps.get(0).get("carriage_no").toString();

        //后序分配，防止重复分配
        String seatNo = maps.get(0).get("seat_count").toString();

        orderService.isCheck(userEntity, carriages_no, seatNo, startStation, endStation, trainNumber, price);
        //选择座位等级
        return Result.ok();
    }
    @ApiOperation(value = "退票")
    @GetMapping("/refundTicket")

    public Result refundTicket(@RequestParam String orderId){

        orderService.removeById(orderId);
        return Result.ok();
    }
}
